package com.sinodata.bsm.center.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.sinodata.bsm.center.cache.AttributeCache;
import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.cache.PropertyCache;
import com.sinodata.bsm.center.cache.ResTypeCache;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.vo.Attribute;
import com.sinodata.bsm.common.vo.EventType;
import com.sinodata.bsm.common.vo.Property;
import com.sinodata.bsm.common.vo.ResType;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-22 PM 3:44:17     liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ResTypeBean {

    private static ResTypeCache resTypeCache = SpringContextHolder.getBean(ResTypeCache.class);
    private static AttributeCache attributeCache = SpringContextHolder.getBean(AttributeCache.class);
    private static PropertyCache propertyCache = SpringContextHolder.getBean(PropertyCache.class);
    private static EventTypeCache eventTypeCache = SpringContextHolder.getBean(EventTypeCache.class);
    private ResType resType;

    /**
     * 默认构造方法，构建业务对象时建议使用传入VO对象的构建方法
     */
    private ResTypeBean() {

    }

    /**
     * 资源类别业务对象构建方法
     * 
     * @param resType 资源类别VO对象
     */
    private ResTypeBean(ResType resType) {
        this.resType = resType;
    }

    /**
     * 获取资源类别业务对象
     * 
     * @param resTypeId
     * @return
     */
    public static ResTypeBean get(Long resTypeId) {
        return new ResTypeBean(resTypeCache.get(resTypeId));
    }

    /**
     * 获取资源类别POJO对象
     * 
     * @return
     */
    public ResType resType() {
        return this.resType;
    }

    public List<Attribute> attributes() {
        List<Attribute> attrList = new ArrayList<Attribute>();
        List<Attribute> aList = attributeCache.getByResTypeId(resType.getId());
        if (aList != null) {
            attrList.addAll(aList);
        }
        ResTypeBean p = parent();
        while (p != null) {
            for (Attribute attr : p.attributes()) {
                if (!attrList.contains(attr)) {
                    attrList.add(attr);
                }
            }
            p = p.parent();
        }
        Collections.sort(attrList);
        return attrList;
    }

    public List<Attribute> attributes(boolean extend) {
        if (extend) {
            return attributes();
        } else {
            return attributeCache.getByResTypeId(resType.getId());
        }
    }

    public List<Property> properties() {
        List<Property> propList = new ArrayList<Property>();
        List<Property> pList = propertyCache.getByResTypeId(resType.getId());
        if (pList != null) {
            propList.addAll(pList);
        }
        ResTypeBean p = parent();
        while (p != null) {
            for (Property prop : p.properties()) {
                if (!propList.contains(prop)) {
                    propList.add(prop);
                }
            }
            p = p.parent();
        }
        return propList;
    }

    public List<EventType> eventType() {
        List<EventType> eventTypeList = new ArrayList<EventType>();
        List<EventType> eList = eventTypeCache.findByResTypeId(resType.getId());
        if (eList != null) {
            eventTypeList.addAll(eList);
        }
        ResTypeBean p = parent();
        while (p != null) {
            for (EventType eventType : p.eventType()) {
                if (!eventTypeList.contains(eventType)) {
                    eventTypeList.add(eventType);
                }
            }
            p = p.parent();
        }
        return eventTypeList;
    }

    public List<Property> properties(boolean extend) {
        if (extend) {
            return properties();
        } else {
            return propertyCache.getByResTypeId(resType.getId());
        }
    }

    public ResTypeBean parent() {
        Long parentId = resType.getParentId();
        if (parentId == null || parentId <= 0) {
            return null;
        }
        return ResTypeBean.get(parentId);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return resType.getName();
    }

}